package com.zretc.kscxmz.controller;

import com.github.pagehelper.PageInfo;
import com.zretc.kscxmz.entity.*;
import com.zretc.kscxmz.entity.Process;
import com.zretc.kscxmz.services.ProcessService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartRequest;

import javax.persistence.criteria.CriteriaBuilder;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/process")
public class ProcessController {
    private Logger logger = LoggerFactory.getLogger(ProcessController.class);

    @Autowired
    private ProcessService processService;

    @RequestMapping("/costWay")
    @ResponseBody
    public List<CostWay> getCostWayList(){
        List<CostWay> costWayList = processService.getCostWayList();
        return costWayList;
    }
    @RequestMapping("/evectionType")
    @ResponseBody
    public List<EvectionType> getEvectionTypeList(){
        List<EvectionType> evectionTypeList = processService.getEvectionTypeList();
        return evectionTypeList;
    }
    @RequestMapping("/overtimeType")
    @ResponseBody
    public List<OvertimeType> getOvertimeTypeList(){
        List<OvertimeType> overtimeTypeList = processService.getOvertimeTypeList();
        return  overtimeTypeList;
    }
    @RequestMapping("/urgency")
    @ResponseBody
    public List<Urgency> getUrgencyList(){
        List<Urgency> urgencyList = processService.getUrgencyList();
        return urgencyList;
    }
    @RequestMapping("/vacateType")
    @ResponseBody
    public List<VacateType> getVacateTypeList(){
        List<VacateType> vacateTypeList = processService.getVacateTypeList();
        return vacateTypeList;
    }
    @RequestMapping("/processList")
    @ResponseBody
    public PageInfo<Process> getProcessList(@RequestParam("proposerId")String proposerId,
                                            @RequestParam("pageNum") int pageNum,
                                            @RequestParam("pageSize") int pageSize){
        Integer proposerId1=null;
        if (proposerId==""){
            proposerId1=null;
        }else {
            proposerId1=Integer.parseInt(proposerId);
        }
        PageInfo<Process> processList = processService.getProcessList(proposerId1, pageNum, pageSize);

        return processList;
    }
    @RequestMapping("/addCostProcess" )
    @ResponseBody
    public int insertProcess1(MultipartHttpServletRequest request) throws IOException {
        Integer processTypeId=Integer.parseInt(request.getParameter("processTypeId"));
        String processTitle = request.getParameter("processTitle");
        Integer urgencyId= Integer.parseInt(request.getParameter("urgencyId"));
        Integer proposerId = Integer.parseInt(request.getParameter("proposerId"));
        Double costMoney = Double.parseDouble(request.getParameter("costMoney"));
        Integer wayId = Integer.parseInt(request.getParameter("wayId"));
        String processDetail = request.getParameter("processDetail");
        MultipartFile costBill = request.getFile("costBill");
        costBill.transferTo(new File("E:\\files"+costBill.getOriginalFilename()));
        Integer assessorId = Integer.parseInt(request.getParameter("assessorId"));
        Process process = new Process();
        process.setProcessTypeId(processTypeId);
        process.setProcessTitle(processTitle);
        process.setUrgencyId(urgencyId);
        process.setProposerId(proposerId);
        process.setCostMoney(costMoney);
        process.setWayId(wayId);
        process.setCostBill(costBill.getOriginalFilename());
        process.setProcessDetail(processDetail);
        process.setAssessorId(assessorId);
        int i = processService.submitProcess(process);
        return i;
    }
    @RequestMapping("/addEvectionProcess")
    @ResponseBody
    public int insertProcess2(MultipartHttpServletRequest request){
        Integer processTypeId=Integer.parseInt(request.getParameter("processTypeId"));
        String processTitle = request.getParameter("processTitle");
        Integer urgencyId= Integer.parseInt(request.getParameter("urgencyId"));
        Integer proposerId = Integer.parseInt(request.getParameter("proposerId"));
        Integer evectionTypeId = Integer.parseInt(request.getParameter("evectionTypeId"));
        String startDate = request.getParameter("startDate");
        String endDate = request.getParameter("endDate");
        String processDetail = request.getParameter("processDetail");
        Integer assessorId = Integer.parseInt(request.getParameter("assessorId"));
        Process process = new Process();
        process.setProcessTypeId(processTypeId);
        process.setProcessTitle(processTitle);
        process.setUrgencyId(urgencyId);
        process.setProposerId(proposerId);
        process.setEvectionTypeId(evectionTypeId);
        process.setStartDate(startDate);
        process.setEndDate(endDate);
        process.setProcessDetail(processDetail);
        process.setAssessorId(assessorId);
        int i = processService.submitProcess(process);
        return i;
    }
    @RequestMapping("/addOvertimeProcess")
    @ResponseBody
    public int insertProcess3(MultipartHttpServletRequest request){
        Integer processTypeId=Integer.parseInt(request.getParameter("processTypeId"));
        String processTitle = request.getParameter("processTitle");
        Integer urgencyId= Integer.parseInt(request.getParameter("urgencyId"));
        Integer proposerId = Integer.parseInt(request.getParameter("proposerId"));
        Integer overtimeTypeId = Integer.parseInt(request.getParameter("overtimeTypeId"));
        String overtimeTime = request.getParameter("overtimeTime");
        String processDetail = request.getParameter("processDetail");
        Integer assessorId = Integer.parseInt(request.getParameter("assessorId"));
        Process process = new Process();
        process.setProcessTypeId(processTypeId);
        process.setProcessTitle(processTitle);
        process.setUrgencyId(urgencyId);
        process.setProposerId(proposerId);
        process.setOvertimeTypeId(overtimeTypeId);
        process.setOvertimeTime(overtimeTime);
        process.setProcessDetail(processDetail);
        process.setAssessorId(assessorId);
        int i = processService.submitProcess(process);
        return i;
    }
    @RequestMapping("/addPositiveProcess")
    @ResponseBody
    public int insertProcess4(MultipartHttpServletRequest request){
        Integer processTypeId=Integer.parseInt(request.getParameter("processTypeId"));
        String processTitle = request.getParameter("processTitle");
        Integer urgencyId= Integer.parseInt(request.getParameter("urgencyId"));
        Integer proposerId = Integer.parseInt(request.getParameter("proposerId"));
        String startDate = request.getParameter("startDate");
        String endDate = request.getParameter("endDate");
        String processDetail = request.getParameter("processDetail");
        Integer assessorId = Integer.parseInt(request.getParameter("assessorId"));
        Process process = new Process();
        process.setProcessTypeId(processTypeId);
        process.setProcessTitle(processTitle);
        process.setUrgencyId(urgencyId);
        process.setProposerId(proposerId);
        process.setStartDate(startDate);
        process.setEndDate(endDate);
        process.setProcessDetail(processDetail);
        process.setAssessorId(assessorId);
        int i = processService.submitProcess(process);
        return i;
    }
    @RequestMapping("/addVacateProcess")
    @ResponseBody
    public int insertProcess5(MultipartHttpServletRequest request){
        Integer processTypeId=Integer.parseInt(request.getParameter("processTypeId"));
        String processTitle = request.getParameter("processTitle");
        Integer urgencyId= Integer.parseInt(request.getParameter("urgencyId"));
        Integer proposerId = Integer.parseInt(request.getParameter("proposerId"));
        Integer vacateTypeId = Integer.parseInt(request.getParameter("vacateTypeId"));
        String  vacateDays = request.getParameter("vacateDays");
        String startDate = request.getParameter("startDate");
        String endDate = request.getParameter("endDate");
        String processDetail = request.getParameter("processDetail");
        Integer assessorId = Integer.parseInt(request.getParameter("assessorId"));
        String phoneNumber = request.getParameter("phoneNumber");
        Process process = new Process();
        process.setProcessTypeId(processTypeId);
        process.setProcessTitle(processTitle);
        process.setUrgencyId(urgencyId);
        process.setProposerId(proposerId);
        process.setVacateTypeId(vacateTypeId);
        process.setVacateDays(vacateDays);
        process.setStartDate(startDate);
        process.setEndDate(endDate);
        process.setProcessDetail(processDetail);
        process.setAssessorId(assessorId);
        process.setPhoneNumber(phoneNumber);
        int i = processService.submitProcess(process);
        return i;
    }
    @RequestMapping("/addResignProcess")
    @ResponseBody
    public int insertProcess6(MultipartHttpServletRequest request){
        Integer processTypeId=Integer.parseInt(request.getParameter("processTypeId"));
        String processTitle = request.getParameter("processTitle");
        Integer urgencyId= Integer.parseInt(request.getParameter("urgencyId"));
        Integer proposerId = Integer.parseInt(request.getParameter("proposerId"));
        String processDetail = request.getParameter("processDetail");
        String resignDate = request.getParameter("resignDate");
        Integer assessorId = Integer.parseInt(request.getParameter("assessorId"));
        Process process = new Process();
        process.setProcessTypeId(processTypeId);
        process.setProcessTitle(processTitle);
        process.setUrgencyId(urgencyId);
        process.setProposerId(proposerId);
        process.setProcessDetail(processDetail);
        process.setResignDate(resignDate);
        process.setAssessorId(assessorId);
        int i = processService.submitProcess(process);
        return i;
    }

    @RequestMapping("deleteProcess")
    @ResponseBody
    public int deleteProcess(@RequestParam("processId")String processId){

        int i = processService.deleteProcessById(Integer.parseInt(processId));
        return i;
    }


    @RequestMapping("updateProcess")
    @ResponseBody
    public int updateProcess(@RequestParam("processId")String processId,
                             @RequestParam("processStatus")String processStatus){

        int i = processService.modifyStatusById(Integer.parseInt(processId),Integer.parseInt(processStatus));
        return i;
    }
}
